home *** CD-ROM | disk | FTP | other *** search
- Path: news.logicon.com!newsmaster@klee
- From: kkolda@logicon.com (Kenneth D. Kolda)
- Newsgroups: comp.lang.c++
- Subject: Re: Derivation and calling virtual functions
- Date: 29 Mar 1996 18:42:23 GMT
- Organization: Logicon Operating Systems
- Message-ID: <4jhauf$pf4@piper.logicon.com>
- References: <graphix.828032689@spiff.cc.iastate.edu>
- NNTP-Posting-Host: 137.51.122.161
- Mime-Version: 1.0
-
- In article <graphix.828032689@spiff.cc.iastate.edu>, graphix@iastate.edu
- says...
- >
- > Say we have the following:
- >
- >class Base {
- >public:
- > virtual void func() { // some stuff }
- > virtual void write() { func(); };
- >};
- >
- >class Derived : public Base {
- >public:
- > void func() { // some stuff }
- > write(); { Base::func(); }
- >};
- >
- >Derived inst;
- >
- > Now, when I call inst.write() it in turn calls Bass::write() which in
- >turn calls Base::func(). Is there a way to instead have it call
- >Derived::func() if the instance is of type Derived? I hoped the
- >virtual keyword would help in this case.
- >
- > Thanks.
- >
- >--
- >Kent Vander Velden
- >graphix@iastate.edu
- >
-
-
- First, I'm going to assume that you have some typos -- namely that
- Derived::write() should be as follows:
-
- void Derived::write()
- { Base::write(); // instead of Base::func() }
-
- Now, when you call inst.write(), it will call Base::write() (as you
- described) BUT it *should* then call Derived::func(). If your compiler
- causes Base::func() to be called, there's a problem since the virtual
- keyword should definitely trip the call into the derived class.
-
- The other possibility is that the prototypes of your func()
- methods are not exactly the same (all parameter types and return value
- type). In this case the virtual mechanism gets overridden and what you
- described would occur.
-
- Ken Kolda
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-